interface Params {
  x: number;
  y: number;
  size: { width: number; height: number };
  colors: Uint8ClampedArray;
  repleceColor: number[]; //初始被替换的色值
  targetColor: string; //目标色值
  precision: number; //精度值
}

const workercode = () => {
  //判断一个的差距
  const compareColor = (numA: number, numB: number, precision: number) => {
    return Math.abs(numA - numB) < precision;
  };
  //判断批量替换色值的差距
  const isDiff = (
    startColor: number[], //初始要求替换的色值
    nextColor: number[], //影响周边范围色值
    precision: number
  ) => {
    const res =
      compareColor(startColor[0], nextColor[0], precision) &&
      compareColor(startColor[1], nextColor[1], precision) &&
      compareColor(startColor[2], nextColor[2], precision) &&
      compareColor(startColor[3], nextColor[3], precision);
    return !res;
  };
  const hexToRGBA = (
    hex: string
  ): {
    r: number;
    g: number;
    b: number;
    a: number;
  } => {
    // 确保输入的hex是有效的，并去掉可能存在的#号
    hex = hex.replace("#", "");
    // 分解hex字符串为RGBA值
    let r = parseInt(hex.substring(0, 2), 16);
    let g = parseInt(hex.substring(2, 4), 16);
    let b = parseInt(hex.substring(4, 6), 16);
    let a = 1;
    // 检查hex的长度是否为8位
    if (hex.length === 8) {
      const alpha = parseInt(hex.substring(6, 8), 16) / 255; // 将16进制的alpha值转换为0到1之间的浮点数
      a = Number(alpha.toFixed(2));
    }

    // 返回rgba字符串
    return { r, g, b, a };
  };
  const point2Index = (x: number, y: number, canvasWidth: number) =>
    (y * canvasWidth + x) * 4;
  const setColor = ({
    x,
    y,
    size,
    colors,
    repleceColor,
    targetColor,
    precision,
  }: Params): { status: boolean; colors: Uint8ClampedArray } => {
    const { r, g, b, a } = hexToRGBA(targetColor);
    const i = point2Index(x, y, size.width);
    const thisColor: number[] = [
      colors[i],
      colors[i + 1],
      colors[i + 2],
      colors[i + 3],
    ];
    if (x < 0 || x >= size.width || x < 0 || y >= size.height) {
      console.log("超出范围");
      return {
        status: false,
        colors,
      };
    }
    const isSame =
      JSON.stringify(thisColor) ===
      JSON.stringify([r, g, b, Math.round(a * 255)]);
    //当前替换色已经和目标色值一样
    if (isSame) {
      console.log("当前替换色已经和目标色值一样");
      return {
        status: false,
        colors,
      };
    }
    const isReplace = isDiff(repleceColor, thisColor, precision);
    // 初始替换色和要被替换的色相差不能超过precision
    if (isReplace) {
      console.log("超过precision");
      return {
        status: false,
        colors,
      };
    }
    colors[i] = r;
    colors[i + 1] = g;
    colors[i + 2] = b;
    colors[i + 3] = Math.round(a * 255);
    return {
      status: true,
      colors,
    };
  };
  const doSomething = (params: Params) => {
    return setColor(params);
  };
  self.onmessage = function (e) {
    const res = doSomething(e.data);
    self.postMessage(res);
  };
};

let code = workercode.toString();
code = code.substring(code.indexOf("{") + 1, code.lastIndexOf("}"));

const blob = new Blob([code], { type: "application/javascript" });
const worker_setColor = URL.createObjectURL(blob);

export default worker_setColor;
